package com.hbx.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;

/*
 * @Author:hbx
 * 
 * */
@Component
@Slf4j
public class Md5Util {
    /*利用MD5进行加密*/
    public String EncoderByMd5(String str) {
        try {
            //确定计算方法
            MessageDigest md5 =	MessageDigest.getInstance("MD5");
            BASE64Encoder base64en = new BASE64Encoder();
            //加密后的字符串
            String newstr = base64en.encode(md5.digest(str.getBytes("utf-8")));
            return newstr;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【MD5加密失败】");
        }
        return null;
    }

    /**判断用户密码是否正确 newpasswd 用户输入的密码 oldpasswd 正确密码*/
    public boolean checkpassword(String newpasswd, String oldpasswd){
        if(EncoderByMd5(newpasswd).equals(oldpasswd))
            return true;
        else
            return false;
    }
}
